package com.rex.saas.controller;

import com.alibaba.fastjson.JSONObject;
import com.rex.saas.bean.*;
import com.rex.saas.bean.request.PKPropertyAddRequest;
import com.rex.saas.bean.request.ProductKeyAddRequest;
import com.rex.saas.constants.Constants;
import com.rex.saas.db.entity.SaasTslModel;
import com.rex.saas.db.entity.SaasTslModelProperty;
import com.rex.saas.enums.TslEventTypeEnum;
import com.rex.saas.service.CategoryService;
import com.rex.saas.service.DeviceService;
import com.rex.saas.service.TslModelService;
import com.rex.saas.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private TslModelService tslModelService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DeviceService deviceService;

    @GetMapping("/init")
    public BaseResponse init(HttpServletRequest request,  @RequestParam String path){
        log.info("init ===> path:{}", path);
        String token = request.getHeader("token");
        List<String> roles = JwtUtils.getRole(token);
        if (!roles.contains(Constants.TENANT_ADMIN)){
            log.error("init , user has not permission , path:{}", path);
            return BaseResponse.error("一般用户没有权限", ResultCode.PARAM_ERROR);
        }

        return BaseResponse.ok(tslModelService.init(path, token));
    }

    @PostMapping("/update/property")
    public BaseResponse updateProperty(HttpServletRequest request,  @RequestBody TslProperty tslProperty){
        log.info("updateProperty ===> tslProperty:{}", tslProperty);
        String token = request.getHeader("token");
        List<String> roles = JwtUtils.getRole(token);
        if (!roles.contains(Constants.TENANT_ADMIN)){
            log.error("updateProperty , user has not permission , tslProperty:{}", tslProperty);
            return BaseResponse.error("一般用户没有修改产品权限", ResultCode.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(tslProperty.getTslModelId())){
            log.error("updateProperty productKey is null, tslProperty:{}", tslProperty);
            return BaseResponse.error("产品Key不能为空", ResultCode.PARAM_ERROR);
        }
        SaasTslModelProperty tslModelProperty = tslModelService.queryTslModelPropertyById(tslProperty.getId());
        if (tslModelProperty == null){
            log.error("updateProperty , tslModelProperty is null, tslProperty:{}", tslProperty);
            return BaseResponse.error("属性不存在", ResultCode.PARAM_ERROR);
        }
        if(!tslModelProperty.getTslModelId().equals(tslProperty.getTslModelId())){
            log.error("updateProperty , tslModelProperty is not belong to tslModel, tslProperty:{}", tslProperty);
            return BaseResponse.error("属性不属于该产品", ResultCode.PARAM_ERROR);
        }
        if (!tslModelProperty.getIdentifier().equals(tslProperty.getIdentifier())){
            log.error("updateProperty , tslModelProperty identifier is not equal tslProperty identifier, tslProperty:{}", tslProperty);
            return BaseResponse.error("属性标识符不能修改", ResultCode.PARAM_ERROR);
        }
        try {
            JSONObject dataType = JSONObject.parseObject(tslProperty.getDataType());
        }catch (Exception e){
            log.error("updateProperty property dataType is not json, tslProperty:{}", tslProperty);
            return BaseResponse.error("属性数据格式错误", ResultCode.PARAM_ERROR);
        }
        SaasTslModel tslModel = tslModelService.queryTslModelById(tslProperty.getTslModelId());
        if (tslModel == null){
            log.error("updateProperty , tslModel is null, tslProperty:{}", tslProperty);
            return BaseResponse.error("产品不存在", ResultCode.PARAM_ERROR);
        }
        return BaseResponse.ok(tslModelService.updateProperty(tslProperty));
    }

    @GetMapping("/delete")
    public BaseResponse delete(HttpServletRequest request,  @RequestParam String productKey){
        log.info("delete ===> productKey:{}", productKey);
        String token = request.getHeader("token");
        List<String> roles = JwtUtils.getRole(token);
        if (!roles.contains(Constants.TENANT_ADMIN)){
            log.error("delete , user has not permission , productKey:{}", productKey);
            return BaseResponse.error("一般用户没有删除产品权限", ResultCode.PARAM_ERROR);
        }
        return BaseResponse.ok(tslModelService.delete(productKey));
    }

    @GetMapping("/detail")
    public BaseResponse detail(HttpServletRequest request,  @RequestParam String productKey){
        log.info("detail ===> productKey:{}", productKey);
        String token = request.getHeader("token");
        return BaseResponse.ok(tslModelService.detail(productKey, JwtUtils.getTenantId( token)));
    }
    @PostMapping("/add")
    public BaseResponse add(HttpServletRequest request,  @RequestBody ProductKeyAddRequest productKeyAddRequest){
        log.info("add ===> productKeyRequest:{}", productKeyAddRequest);
        String token = request.getHeader("token");
        List<String> roles = JwtUtils.getRole(token);
        if (!roles.contains(Constants.TENANT_ADMIN)){
            log.error("add , user has not permission , productKeyRequest:{}", productKeyAddRequest);
            return BaseResponse.error("一般用户没有添加产品权限", ResultCode.PARAM_ERROR);
        }
        if(StringUtils.isEmpty(productKeyAddRequest.getType())){
            log.error("add type is null, productKeyRequest:{}", productKeyAddRequest);
            return BaseResponse.error("节点类型不能为空", ResultCode.PARAM_ERROR);
        }

        if (StringUtils.isEmpty(productKeyAddRequest.getProductKey())){
            log.error("add productKey is null, productKeyRequest:{}", productKeyAddRequest);
            return BaseResponse.error("产品Key不能为空", ResultCode.PARAM_ERROR);
        }

        if (StringUtils.isEmpty(productKeyAddRequest.getProductName())){
            log.error("add productKey is null, productKeyRequest:{}", productKeyAddRequest);
            return BaseResponse.error("产品名称不能为空", ResultCode.PARAM_ERROR);
        }
        DeviceProfileBasicData category = categoryService.getDetailsById(token, productKeyAddRequest.getProfileId());
        if (category != null){
            log.error("add category is not exist, productKeyRequest:{}", productKeyAddRequest);
            return BaseResponse.error("品类名称不存在", ResultCode.PARAM_ERROR);
        }
        List<SaasTslModel> pks = tslModelService.queryTslModelByPK(productKeyAddRequest.getProductKey(), JwtUtils.getTenantId(token));
        if (!CollectionUtils.isEmpty(pks)){
            log.error("add productKey is exist, productKeyRequest:{}", productKeyAddRequest);
            return BaseResponse.error("产品Key已存在", ResultCode.PARAM_ERROR);
        }
        return BaseResponse.ok(tslModelService.addProductModel(productKeyAddRequest, JwtUtils.getTenantId(token)));
    }
    @PostMapping("/configProperty")
    public BaseResponse configProperty(HttpServletRequest request, @RequestBody PKPropertyAddRequest pkPropertyAddRequest){
        log.info("configProperty ===> pkPropertyAddRequest:{}", pkPropertyAddRequest);
        if(StringUtils.isEmpty(pkPropertyAddRequest.getProductKey())){
            log.error("configProperty productKey is null, pkPropertyAddRequest:{}", pkPropertyAddRequest);
            return BaseResponse.error("产品Key不能为空", ResultCode.PARAM_ERROR);
        }
        if(CollectionUtils.isEmpty(pkPropertyAddRequest.getProperties())){
            log.error("configProperty properties is empty, pkPropertyAddRequest:{}", pkPropertyAddRequest);
            return BaseResponse.error("属性不能为空", ResultCode.PARAM_ERROR);
        }
        String token = request.getHeader("token");
        List<SaasTslModel> tslModels = tslModelService.queryTslModelByPK(pkPropertyAddRequest.getProductKey(), JwtUtils.getTenantId(token));
        if (CollectionUtils.isEmpty(tslModels)){
            log.error("configProperty product is null, pkPropertyAddRequest:{}", pkPropertyAddRequest);
            return BaseResponse.error("产品不存在", ResultCode.PARAM_ERROR);
        }
        Long modelId = tslModels.get(0).getId();
        for (int i = 0; i < pkPropertyAddRequest.getProperties().size(); i++) {
            TslProperty tslProperty = pkPropertyAddRequest.getProperties().get( i);
            if(StringUtils.isEmpty(tslProperty.getIdentifier())){
                log.error("configProperty property identifier is null, tslProperty:{}", tslProperty);
                return BaseResponse.error("标识符不能为空", ResultCode.PARAM_ERROR);
            }
            List<SaasTslModelProperty> props = tslModelService.queryPropertyByIdentifier(tslProperty.getIdentifier(), modelId);
            if (!CollectionUtils.isEmpty(props)){
                log.error("configProperty property identifier is exist, tslProperty:{}", tslProperty);
                return BaseResponse.error("属性标识符已存在", ResultCode.PARAM_ERROR);
            }
            if(StringUtils.isEmpty(tslProperty.getName())){
                log.error("configProperty property name is null, tslProperty:{}", tslProperty);
                return BaseResponse.error("名称不能为空", ResultCode.PARAM_ERROR);
            }
            if(StringUtils.isEmpty(tslProperty.getAccessMode())){
                log.error("configProperty property accessMode is null, tslProperty:{}", tslProperty);
                return BaseResponse.error("访问模式不能为空", ResultCode.PARAM_ERROR);
            }
            if(StringUtils.isEmpty(tslProperty.getDataType())){
                log.error("configProperty property dataType is null, tslProperty:{}", tslProperty);
                return BaseResponse.error("数据类型不能为空", ResultCode.PARAM_ERROR);
            }
            try {
                JSONObject dataType = JSONObject.parseObject(tslProperty.getDataType());
            }catch (Exception e){
                log.error("configProperty property dataType is not json, tslProperty:{}", tslProperty);
                return BaseResponse.error("property datatype数据类型格式错误", ResultCode.PARAM_ERROR);
            }
            Boolean flag = tslModelService.configProperty(tslProperty);
            if (!flag){
                log.error("configProperty property error, tslProperty:{}", tslProperty);
                return BaseResponse.error("属性配置失败", ResultCode.PARAM_ERROR);
            }
        }
        return BaseResponse.ok();
    }

    @PostMapping("/add2")
    public BaseResponse add2(HttpServletRequest request,  @RequestBody ProductModel productModel){
        log.info("add ===> productModel:{}", productModel);
        String token = request.getHeader("token");
        List<String> roles = JwtUtils.getRole(token);
        if (!roles.contains(Constants.TENANT_ADMIN)){
            log.error("add , user has not permission , productModel:{}", productModel);
            return BaseResponse.error("一般用户没有删除产品权限", ResultCode.PARAM_ERROR);
        }

        if(StringUtils.isEmpty(productModel.getProfile())){
            log.error("list profile is null, productModel:{}", productModel);
            return BaseResponse.error("品类不能为空", ResultCode.PARAM_ERROR);
        }
        if(StringUtils.isEmpty(productModel.getType())){
            log.error("list type is null, productModel:{}", productModel);
            return BaseResponse.error("节点类型不能为空", ResultCode.PARAM_ERROR);
        }

        if (StringUtils.isEmpty(productModel.getProductKey())){
            log.error("list productKey is null, productModel:{}", productModel);
            return BaseResponse.error("产品Key不能为空", ResultCode.PARAM_ERROR);
        }

        List<TslProperty> properties = productModel.getProperties();
        if(!CollectionUtils.isEmpty(properties)){
            for(TslProperty tslProperty : properties){
                if(StringUtils.isEmpty(tslProperty.getIdentifier())){
                    log.error("list property identifier is null, tslProperty:{}", tslProperty);
                    return BaseResponse.error("标识符不能为空", ResultCode.PARAM_ERROR);
                }
                if(StringUtils.isEmpty(tslProperty.getName())){
                    log.error("list property name is null, tslProperty:{}", tslProperty);
                    return BaseResponse.error("名称不能为空", ResultCode.PARAM_ERROR);
                }
                if(StringUtils.isEmpty(tslProperty.getAccessMode())){
                    log.error("list property accessMode is null, tslProperty:{}", tslProperty);
                    return BaseResponse.error("访问模式不能为空", ResultCode.PARAM_ERROR);
                }
                if(StringUtils.isEmpty(tslProperty.getDataType())){
                    log.error("list property dataType is null, tslProperty:{}", tslProperty);
                    return BaseResponse.error("数据类型不能为空", ResultCode.PARAM_ERROR);
                }
                try {
                    JSONObject dataType = JSONObject.parseObject(tslProperty.getDataType());
                }catch (Exception e){
                    log.error("list property dataType is not json, tslProperty:{}", tslProperty);
                    return BaseResponse.error("property datatype数据类型格式错误", ResultCode.PARAM_ERROR);
                }
            }
            List<String> identifiers = properties.stream().sorted().map(TslProperty::getIdentifier).collect(Collectors.toList());
            for (int i = 0; i < identifiers.size() - 1; i++) {
                if (identifiers.get(i).equals(identifiers.get(i + 1))) {
                    log.error("tsl property identifier is duplicate, identifiers:{}", identifiers);
                    return BaseResponse.error("属性标识符不能重复", ResultCode.PARAM_ERROR);
                }
            }
        }

        List<TslService> services = productModel.getServices();
        if(!CollectionUtils.isEmpty(services)){
            List<String> identifiers = null;
            for (TslService tslService : services){
                if(StringUtils.isEmpty(tslService.getIdentifier())){
                    log.error("list services identifier is null, tslService:{}", tslService);
                    return BaseResponse.error("标识符不能为空", ResultCode.PARAM_ERROR);
                }
                if(StringUtils.isEmpty(tslService.getName())){
                    log.error("list services name is null, tslService:{}", tslService);
                    return BaseResponse.error("名称不能为空", ResultCode.PARAM_ERROR);
                }
                if(StringUtils.isEmpty(tslService.getCallType())){
                    log.error("list services callType is null, tslService:{}", tslService);
                    return BaseResponse.error("调用类型不能为空", ResultCode.PARAM_ERROR);
                }
                if(StringUtils.isEmpty(tslService.getMethod())){
                    log.error("list services method is null, tslService:{}", tslService);
                    return BaseResponse.error("方法不能为空", ResultCode.PARAM_ERROR);
                }

                List<TslServiceData> inputData = tslService.getInputData();
                if(!CollectionUtils.isEmpty(inputData)){
                    for(TslServiceData tslServiceData : inputData){
                        if(StringUtils.isEmpty(tslServiceData.getIdentifier())){
                            log.error("list services inputData identifier is null, tslServiceData:{}", tslServiceData);
                            return BaseResponse.error("标识符不能为空", ResultCode.PARAM_ERROR);
                        }
                        if(StringUtils.isEmpty(tslServiceData.getName())){
                            log.error("list services inputData name is null, tslServiceData:{}", tslServiceData);
                            return BaseResponse.error("名称不能为空", ResultCode.PARAM_ERROR);
                        }
                        if(StringUtils.isEmpty(tslServiceData.getDataType())){
                            log.error("list services inputData dataType is null, tslServiceData:{}", tslServiceData);
                            return BaseResponse.error("数据类型不能为空", ResultCode.PARAM_ERROR);
                        }
                        try {
                            JSONObject dataType = JSONObject.parseObject(tslServiceData.getDataType());
                        }catch (Exception e){
                            log.error("list services inputData dataType is not json, tslServiceData:{}", tslServiceData);
                            return BaseResponse.error("service inputData数据类型格式错误", ResultCode.PARAM_ERROR);
                        }
                    }
                    identifiers = inputData.stream().sorted().map(TslServiceData::getIdentifier).collect(Collectors.toList());
                    for (int i = 0; i < identifiers.size() - 1; i++) {
                        if (identifiers.get(i).equals(identifiers.get(i + 1))) {
                            log.error("tsl services inputData identifier is duplicate, identifiers:{}", identifiers);
                            return BaseResponse.error("服务inputData标识符不能重复", ResultCode.PARAM_ERROR);
                        }
                    }
                }

                List<TslServiceData> outputData = tslService.getInputData();
                if(!CollectionUtils.isEmpty(outputData)){
                    for(TslServiceData tslServiceData : outputData){
                        if (StringUtils.isEmpty(tslServiceData.getIdentifier())){
                            log.error("list services outputData identifier is null, tslServiceData:{}", tslServiceData);
                            return BaseResponse.error("标识符不能为空", ResultCode.PARAM_ERROR);
                        }
                        if (StringUtils.isEmpty(tslServiceData.getName())){
                            log.error("list services outputData name is null, tslServiceData:{}", tslServiceData);
                            return BaseResponse.error("名称不能为空", ResultCode.PARAM_ERROR);
                        }
                        if (StringUtils.isEmpty(tslServiceData.getDataType())){
                            log.error("list services outputData dataType is null, tslServiceData:{}", tslServiceData);
                            return BaseResponse.error("数据类型不能为空", ResultCode.PARAM_ERROR);
                        }
                        try {
                            JSONObject dataType = JSONObject.parseObject(tslServiceData.getDataType());
                        }catch (Exception e){
                            log.error("list services outputData dataType is not json, tslServiceData:{}", tslServiceData);
                            return BaseResponse.error("service outputData数据类型格式错误", ResultCode.PARAM_ERROR);
                        }
                    }
                    identifiers = outputData.stream().sorted().map(TslServiceData::getIdentifier).collect(Collectors.toList());
                    for (int i = 0; i < identifiers.size() - 1; i++) {
                        if (identifiers.get(i).equals(identifiers.get(i + 1))) {
                            log.error("tsl services outputData identifier is duplicate, identifiers:{}", identifiers);
                            return BaseResponse.error("服务outputData标识符不能重复", ResultCode.PARAM_ERROR);
                        }
                    }
                }
            }
            identifiers = services.stream().sorted().map(TslService::getIdentifier).collect(Collectors.toList());
            for (int i = 0; i < identifiers.size() - 1; i++) {
                if (identifiers.get(i).equals(identifiers.get(i + 1))) {
                    log.error("tsl services identifier is duplicate, identifiers:{}", identifiers);
                    return BaseResponse.error("服务标识符不能重复", ResultCode.PARAM_ERROR);
                }
            }
        }
        List<TslEvent> events = productModel.getEvents();
        if(!CollectionUtils.isEmpty(events)){
            List<String> identifiers = null;
            for (TslEvent tslEvent : events){
                if(StringUtils.isEmpty(tslEvent.getIdentifier())){
                    log.error("list events identifier is null, tslEvent:{}", tslEvent);
                    return BaseResponse.error("标识符不能为空", ResultCode.PARAM_ERROR);
                }
                if(StringUtils.isEmpty(tslEvent.getName())){
                    log.error("list events name is null, tslEvent:{}", tslEvent);
                    return BaseResponse.error("名称不能为空", ResultCode.PARAM_ERROR);
                }
                if(tslEvent.getType()==null
                        || (!tslEvent.getType().equals(TslEventTypeEnum.INFO.getCode())
                        && !tslEvent.getType().equals(TslEventTypeEnum.WARN.getCode())
                        && !tslEvent.getType().equals(TslEventTypeEnum.ERROR.getCode())
                )){
                    log.error("list events type is null, tslEvent:{}", tslEvent);
                    return BaseResponse.error("事件类型异常", ResultCode.PARAM_ERROR);
                }
                if(StringUtils.isEmpty(tslEvent.getMethod())){
                    log.error("list events method is null, tslEvent:{}", tslEvent);
                    return BaseResponse.error("方法不能为空", ResultCode.PARAM_ERROR);
                }
                List<TslEventData> inputData = tslEvent.getInputData();
                if(!CollectionUtils.isEmpty(inputData)){
                    for(TslEventData tslEventData : inputData){
                        if (StringUtils.isEmpty(tslEventData.getIdentifier())){
                            log.error("list events inputData identifier is null, tslEventData:{}", tslEventData);
                            return BaseResponse.error("标识符不能为空", ResultCode.PARAM_ERROR);
                        }
                        if (StringUtils.isEmpty(tslEventData.getName())){
                            log.error("list events inputData name is null, tslEventData:{}", tslEventData);
                            return BaseResponse.error("名称不能为空", ResultCode.PARAM_ERROR);
                        }
                        if (StringUtils.isEmpty(tslEventData.getDataType())){
                            log.error("list events inputData dataType is null, tslEventData:{}", tslEventData);
                            return BaseResponse.error("数据类型不能为空", ResultCode.PARAM_ERROR);
                        }
                        try {
                            JSONObject dataType = JSONObject.parseObject(tslEventData.getDataType());
                        }catch (Exception e){
                            log.error("list events inputData dataType is not json, tslEventData:{}", tslEventData);
                            return BaseResponse.error("event inputData数据类型格式错误", ResultCode.PARAM_ERROR);
                        }
                    }
                    identifiers = inputData.stream().sorted().map(TslEventData::getIdentifier).collect(Collectors.toList());
                    for (int i = 0; i < identifiers.size() - 1; i++) {
                        if (identifiers.get(i).equals(identifiers.get(i + 1))) {
                            log.error("tsl events input inputData identifier is duplicate, identifiers:{}", identifiers);
                            return BaseResponse.error("事件inputData标识符不能重复", ResultCode.PARAM_ERROR);
                        }
                    }
                }

                List<TslEventData> outputData = tslEvent.getOutputData();
                if(!CollectionUtils.isEmpty(outputData)){
                    for(TslEventData tslEventData : outputData){
                        if (StringUtils.isEmpty(tslEventData.getIdentifier())){
                            log.error("list events outputData identifier is null, tslEventData:{}", tslEventData);
                            return BaseResponse.error("标识符不能为空", ResultCode.PARAM_ERROR);
                        }
                        if (StringUtils.isEmpty(tslEventData.getName())){
                            log.error("list events outputData name is null, tslEventData:{}", tslEventData);
                            return BaseResponse.error("名称不能为空", ResultCode.PARAM_ERROR);
                        }
                        if (StringUtils.isEmpty(tslEventData.getDataType())){
                            log.error("list events outputData dataType is null, tslEventData:{}", tslEventData);
                            return BaseResponse.error("数据类型不能为空", ResultCode.PARAM_ERROR);
                        }
                        try {
                            JSONObject dataType = JSONObject.parseObject(tslEventData.getDataType());
                        }catch (Exception e){
                            log.error("list events outputData dataType is not json, tslEventData:{}", tslEventData);
                            return BaseResponse.error("event outputData数据类型格式错误", ResultCode.PARAM_ERROR);
                        }
                    }
                    identifiers = outputData.stream().sorted().map(TslEventData::getIdentifier).collect(Collectors.toList());
                    for (int i = 0; i < identifiers.size() - 1; i++) {
                        if (identifiers.get(i).equals(identifiers.get(i + 1))) {
                            log.error("tsl events outputData identifier is duplicate, identifiers:{}", identifiers);
                            return BaseResponse.error("事件outputData标识符不能重复", ResultCode.PARAM_ERROR);
                        }
                    }
                }
            }
            identifiers = events.stream().sorted().map(TslEvent::getIdentifier).collect(Collectors.toList());
            for (int i = 0; i < identifiers.size() - 1; i++) {
                if (identifiers.get(i).equals(identifiers.get(i + 1))) {
                    log.error("tsl events identifier is duplicate, identifiers:{}", identifiers);
                    return BaseResponse.error("事件标识符不能重复", ResultCode.PARAM_ERROR);
                }
            }
        }

        try {
            tslModelService.add(productModel, JwtUtils.getTenantId(token));
        }catch (Exception e){
            log.error("addStrategyTask error, tslModel:{}", productModel);
            return BaseResponse.error("添加失败:"+e.getMessage(), ResultCode.SYSTEM_ERROR);
        }
        return BaseResponse.ok();
    }
    @GetMapping("/device")
    public BaseResponse device(HttpServletRequest request, @RequestParam String productKey){
        String token = request.getHeader("token");
        List<String> roles = JwtUtils.getRole(token);
        if (!roles.contains(Constants.TENANT_ADMIN)){
            log.error("device , user has not permission , productKey:{}", productKey);
            return BaseResponse.error("一般用户没有查看所有设备权限", ResultCode.PARAM_ERROR);
        }
        String tenantId = JwtUtils.getTenantId(token);
        List<SaasTslModel> tslModels = tslModelService.queryTslModelByPK(productKey, tenantId);
        if (CollectionUtils.isEmpty(tslModels)){
            log.error("device , tslModel is null, productKey:{}", productKey);
            return BaseResponse.error("产品模型不存在", ResultCode.PARAM_ERROR);
        }
        return BaseResponse.ok(deviceService.getDeviceByProductKey(productKey, token));
    }
}

